<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\Job;
use App\Models\FirmProfession;
use App\Models\Post;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class JobController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Job(['user:id,avatar,nickname','post:id,post_name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('post_title','求职岗位');
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('post.post_name','职位');
            $grid->column('profession.name','行业');
            $grid->column('curriculum_vitae')
                 ->display(function ($value){
                     return '<a href="'.$value.'">查看简历</a>';
                 });
            $grid->column('contact_name');
            $grid->column('contact_phone');
            $grid->column('status')
                 ->using([1 => '待跟进',2 => '已跟进'])
                 ->label([1 => 'danger',2 => 'success']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('user.nickname','用户昵称')->width(3);
                $filter->like('post_title','求职岗位')->width(3);
                $filter->equal('post_id')
                      ->select(Post::getOption())
                      ->width(3);
                $filter->equal('firm_profession_id')
                    ->select(FirmProfession::getOption())
                    ->width(3);

                $filter->like('contact_name')->width(3);
                $filter->equal('contact_phone')->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Job(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('post');
            $show->field('contact_name');
            $show->field('exp');
            $show->field('curriculum_vitae');
            $show->field('contact_phone');
            $show->field('expect');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Job(['user:id,nickname,avatar']), function (Form $form) {
            $form->display('id');

            $form->text('post_title','求职岗位')
                ->maxLength(255)
                 ->required();

            $form->select('firm_profession_id','企业行业')
                ->options(FirmProfession::getOption())
                ->required();
            $form->select('post_id','职位')
                 ->options(Post::getOption())
                 ->required();
            $form->text('contact_name')
                 ->required();
            $form->text('contact_phone')
                 ->required();
            $form->text('exp')
                 ->required();
            $form->file('curriculum_vitae')
                ->disk('oss')
                ->autoUpload()
                ->uniqueName()
                ->removable(false)
                ->saveFullUrl()
                ->autoSave(false)
                ->saving(function ($value){
                    return (string)$value;
                });
            $form->distpicker(['province','city','area'],'区域')
                  ->required();


            $form->textarea('expect')
                 ->required();
            $form->radio('status')
                 ->options([1 => '待跟进',2 => '已跟进'])
                 ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
